package bktech.soict.hust.bknote.activity;
import bktech.soict.hust.bknote.R;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import bktech.soict.hust.bknote.activity.sms.About;
import bktech.soict.hust.bknote.activity.sms.QuanLyContact;
import bktech.soict.hust.bknote.activity.sms.QuanLySMS;
import bktech.soict.hust.bknote.activity.sms.SmsHelper;
import bktech.soict.hust.bknote.activity.sms.contact;
import bktech.soict.hust.bknote.activity.sms.sms;

@TargetApi(16)
public class MainActivity extends Activity{
	Button b_contacts,b_search,b_exit,b_setting,b_sms,b_about;
	public static SmsHelper helper = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        helper = new SmsHelper(MainActivity.this);

		Thread createData = new Thread(new Runnable() {

			public void run() {

				Uri uri2 = Uri.parse("content://sms/inbox");
				Cursor d = getContentResolver().query(uri2, null, null, null,
						null);
				if (d.moveToFirst()) {
					for (int i = 1; i <= d.getCount(); i++) {
						int thread_id;
						String address;
						long date;
						String body;
						thread_id = d.getInt(d
								.getColumnIndexOrThrow("thread_id"));
						address = d.getString(d.getColumnIndex("address"));
						date = d.getLong(d.getColumnIndexOrThrow("date"));
						body = d.getString(d.getColumnIndexOrThrow("body"));
						helper.insert_inbox(thread_id, address, date, body);
						d.moveToNext();
					}
					d.close();
				}
				Cursor e = getContentResolver().query(
						ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
						null, null, null, null);
				if (e.moveToFirst()) {
					for (int i = 1; i <= e.getCount(); i++) {
						String number;
						String display_name;
						String temp;
						int contact_id;
						int priority = 0;
						temp = e.getString(e
								.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER));
						display_name = e.getString(e
								.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
						contact_id = e.getInt(e
								.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
						number = new StringBuffer(temp).reverse().toString();
						helper.insert_contact(number, display_name, contact_id,
								priority);
						e.moveToNext();
					}
					e.close();
				}
			}
		});
		createData.start();
        b_contacts = (Button) findViewById(R.id.contacts);
        b_sms = (Button) findViewById(R.id.message);
        b_about=(Button) findViewById(R.id.about);
        b_setting=(Button) findViewById(R.id.b_setting);
        b_about.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i2=new Intent(MainActivity.this,About.class);
				startActivity(i2);

			}
		});
		b_contacts.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				Intent i = new Intent(MainActivity.this, contact.class);
				startActivity(i);
			}
		});
		b_sms.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent i1 = new Intent(MainActivity.this, sms.class);
	            startActivity(i1);

			}
		});
		b_setting.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent receiver = new Intent(MainActivity.this, ReceiverSmsActivity.class);
				startActivity(receiver);
			}
		});
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
